local times = {}

local function timer(name, f)
	local start = os.clock()
	f()
	local stop = os.clock()

	times[name] = (times[name] or 0) + (stop - start)
end

local function show_timers()
	for n,t in pairs(times) do
		print(n .. ": " .. t .. " sec")
	end
end

local node
local Player = {}

local function create_node()

	node = network.create_sync_node({
		balls = {}
	})

	--network.enable_player_list(node, Player)

	local hosting = true

	if hosting then
		node:host(31337)
	else
		node:connect("localhost", 31337)
	end
end

function gfx_test()
	local w, h = 800, 600

	graphics.video(w, h)

	graphics.clear_color(0, 0, 0)

	local snd = sound.load("test.wav")

	local font = graphics.load_font("../Vera.ttf", 64*40)
	local img = graphics.load_image("../face.png")

	local spr = img:to_sprite()
	local x, y = 0, 50

	local frame = 0

	create_node()

	--node:add_player(Player:create())

	while graphics.update() do

		node:update()

		if graphics.buttons[graphics.kbUp] then
			local ch = snd:play()
			sound.set_speed(ch, 0.5)
			sound.set_volume(ch, 0.5)
			sound.set_flags(ch, 0)
			graphics.buttons[graphics.kbUp] = false
		end

		if graphics.buttons[graphics.kbEscape] then
			graphics.close()
		end

		if graphics.buttons[graphics.kbLeft] then
			x = x - 2
		end

		if graphics.buttons[graphics.kbRight] then
			x = x + 2
		end

		graphics.clear()

		graphics.color(1, 1, 1)

		graphics.begin_sprites()

		graphics.text(font, "Best game ever", x/10, 40)
		
		do
			graphics.sprite_centered_rot(spr, x, y, 0)
		end

		--graphics.throttle_fps(30)
		system.sleep(1/30)
		graphics.end_drawing()

		frame = frame + 1

		if bit.band(frame, 31) == 0 then
			print(system.ticks())
			print(system.hires_ticks())
		end
	end

	show_timers()
end